//
//  RegisterViewController.m
//  HeartWorldClient
//
//  Created by Mac on 2017/7/13.
//  Copyright © 2017年 谭明权. All rights reserved.
//

#import "RegisterViewController.h"
#import "CheckVerifiCodeViewController.h"
#import "YCLoginManager.h"
#import "YCProtocolController.h"
#import "YCAlertView.h"


@interface RegisterViewController ()

@property (weak, nonatomic) IBOutlet UITextField *phoneTextField;
@property (weak, nonatomic) IBOutlet UIButton *getVerifiCodeBtn;
@property(nonatomic,strong)CheckVerifiCodeViewController *checkVc;
@property (nonatomic, assign)BOOL isGetCodeAgain;

@end

@implementation RegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.isGetCodeAgain = YES;
    [_phoneTextField becomeFirstResponder];
    [_phoneTextField addTarget:self action:@selector(validationChanged:) forControlEvents:UIControlEventEditingChanged];
    _getVerifiCodeBtn.layer.cornerRadius = 6;
    _getVerifiCodeBtn.clipsToBounds = YES;
    self.title = @"注册";
}

- (IBAction)getVerifiCodeBtnClick:(id)sender {
    
    //进行code的请求
     [self loginWithPhoneNum];

}



-(CheckVerifiCodeViewController *)checkVc{
    if (!_checkVc) {
        _checkVc = [CheckVerifiCodeViewController new];
    }
    return _checkVc;
}

- (void)loginWithPhoneNum {
    
    [self.view endEditing:YES];
    
    if (![YCCommonTool validatePhoneNumber:[_phoneTextField.text cleanTelephoneNumber]]) {
        
        [self presentFailureTips:@"手机号格式不对"];
        return ;
    }
    
    [YCLoginManager shareManager].phoneNumber =  [_phoneTextField.text cleanTelephoneNumber];
 
    self.checkVc.phoneStr = _phoneTextField.text;
    
    self.checkVc.typeFrom = TypeFromRegister;
    
    weakify(self)
    
    [self.checkVc setCountDownBlock:^(NSInteger num){
      
        strongify(self)
        
        self.isGetCodeAgain = (num==0);
        
        [self.getVerifiCodeBtn setTitle:(num>0?[NSString stringWithFormat:@"%@s后重新获取",@(num).stringValue]:@"获取验证码") forState:UIControlStateNormal];
 
    }];
    
    
    self.checkVc.isGetCodeAgain = self.isGetCodeAgain;
    
    [self.navigationController pushViewController:self.checkVc animated:YES];
}
- (IBAction)vipAgreementBtnClick:(id)sender {
    
    YCProtocolController *vc = [[YCProtocolController alloc] init];
    vc.urlString = @"http://wx-heartorg.yk2020.com/weixin/Application/Member/View/app/info.html";
    [self presentViewController:vc animated:YES completion:nil];
    
}


/**
 *  当输入手机号码变化
 *
 *  @param textfied
 */
#pragma mark - 手机号码输入11位时自动跳到密码输入框
-(void)validationChanged:(UITextField *)textfied {
    textfied.text = [textfied.text telephoneNumberFormat];
    if (textfied.text.length >= 13) {
        
        textfied.text = [textfied.text substringWithRange:NSMakeRange(0, 13)];
        //进行code的请求
        [self.view endEditing:YES];
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [self clearKeyboardCorverNotificationAndGesture];
    [self tapGestureHandel];
}


@end
